package com.zjy.basic.service.impl;

import com.zjy.basic.mapper.BaseMapper;
import com.zjy.basic.query.BaseQuery;
import com.zjy.basic.service.BaseService;
import com.zjy.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    /**
     * 新增
     * @param t
     */
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    /**
     * 修改
     * @param t
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //1.查询总数量
        Integer totals = baseMapper.queryCount(baseQuery);
        if (totals==null){
            return new PageList<>();
        }
        //2.查询当前页数据
        List<T> data = baseMapper.queryData(baseQuery);
        return new PageList<>(totals,data);
    }

    @Override
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }
}
